package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.BasicsMessageDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ProductVO;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.BasicsMessageService;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Leon
 * @DateTime: 2021/9/25 10:20
 */
@Service
public class BasicsMessageServiceImpl implements BasicsMessageService {

    @Autowired
    private BasicsMessageDao basicsMessageDao;

    //1、分页查询供应商
    @Override
    public Map<String, Object> getList(Integer page, Integer rows, String supplierName) {
        HashMap<String, Object> map = new HashMap<>();

        //1.1 查询总数total
        Integer total =  basicsMessageDao.getTotal(supplierName);
        map.put("total", total);

        if (page == 0){
            page =1;
        }else {
            page = page;
        }
        System.out.println("page：" + page);

        //1.2根据前端传来的第几页和每页显示多少条，分页显示
        int totalPage = (page - 1) * rows;
        List<Supplier> supplierList = basicsMessageDao.getList(totalPage,rows,supplierName);
        map.put("rows",supplierList);
        return map;
    }

    //2、新增供应商/修改供应商
    @Override
    public ServiceVO addSupplier(Supplier supplier) {

        if (null == supplier.getSupplierId() && null != supplier){
            //2.1 新增供应商
            basicsMessageDao.addSupplier(supplier);
        }else{
            //2.2 修改供应商
            basicsMessageDao.updateSupplier(supplier);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }

    //3、删除供应商
    @Override
    public ServiceVO deleteSupplier(String[] ids) {
        basicsMessageDao.deleteSupplier(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS,null);
    }

    //4、客户管理-分页查询客户列表
    @Override
    public Map<String,Object> getCustomerList(Integer pageNum, Integer pageSize, String customerName) {
        HashMap<String, Object> map = new HashMap<>();
        //4.1 先查询total
        int total = basicsMessageDao.getCustomerTotal(customerName);
        map.put("total", total);

        //4.2查询customer列表分页数据
        pageNum = pageNum == 0 ? 1:pageNum;
        int totalPage = (pageNum - 1) * pageSize;
        List<Customer> customerList =  basicsMessageDao.getCustomerList(totalPage,pageSize,customerName);
        map.put("rows", customerList);
        return map;
    }

    //5、客户管理-添加/修改客户
    @Override
    public ServiceVO addCustomer(Customer customer) {

            //5.1 若有id则为修改
        if (customer.getCustomerId() != null){
            basicsMessageDao.updateCustomer(customer);
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
        }else {
            //5.2 若没有id则为新增
            basicsMessageDao.addCustomer(customer);
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
        }
    }

    //6、客户管理-批量删除客户
    @Override
    public ServiceVO deleteCustomer(String[] ids) {
        basicsMessageDao.deleteCustomer(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    //7、商品管理-树形目录
    @Override
    public List<ProductVO> getProductTree() {

        //构建完成时返回的新对象
        ArrayList<ProductVO> productTree = new ArrayList<>();

        //全取出来
        List<ProductVO> productVOAllList  = basicsMessageDao.getProductTree();

        HashMap<Integer, ProductVO> productVOHashMap = new HashMap<>();
        for (ProductVO productVO : productVOAllList) {
            Integer id = productVO.getId();
            //id为键，对象为值
            productVOHashMap.put(id, productVO);
        }

        for (ProductVO productVO : productVOAllList) {
            Integer pId = productVO.getPId();
         if (pId == -1){
             productTree.add(productVO);
         }else{
             ProductVO product01 = productVOHashMap.get(pId);
             product01.getChildren().add(productVO);
         }
        }

        System.out.println("productTree:"+productTree.toString());
        return productTree;
    }

    //8、商品管理-查询所有商品单位
    @Override
    public Map<String, Object> getAllUnit() {
        HashMap<String, Object> map = new HashMap<>();
        List<Unit> unitList =  basicsMessageDao.getAllUnit();
        map.put("rows", unitList);
        return map;
    }

    //9、商品管理-根据商品类型id和模糊查询获取商品列表
    @Override
    public Map<String, Object> getGoodsList(Integer pageNum, Integer pageSize, String goodsName, Integer goodsTypeId) {

        Map<String, Object> map = new HashMap<>();
        //9.1 总条数,受模糊查询影响
        Integer total = basicsMessageDao.getGoodsTotal(goodsName);
        map.put("total", total);


        //9.2 商品列表带分页
        //9.2.0 根据goodsTypeId获取对象，再会根据pId区分几级分类
        GoodsType goodsType =  basicsMessageDao.selectGoodsType(goodsTypeId);
        if (goodsTypeId != null){
            //9.2.1 查询所有商品
            if (goodsType.getPId() == -1){
                Integer totalPage = (pageNum - 1) * pageSize;
                List<Goods> rows01 =  basicsMessageDao.getGoodListAll(totalPage,pageSize,goodsName,goodsTypeId);
                map.put("rows", rows01);
            }else if (goodsType.getPId() == 1){
                //9.2.2 查询二级商品
                Integer totalPage = (pageNum - 1) * pageSize;
                List<Goods> row02 = basicsMessageDao.getGoodsList02(totalPage,pageSize,goodsName,goodsTypeId);
                map.put("rows",row02);
            }else {
                //9.2.3 查询三级商品
                Integer totalPage = (pageNum - 1) * pageSize;
                List<Goods> row03 = basicsMessageDao.getGoodsList03(totalPage,pageSize,goodsName,goodsTypeId);
                map.put("rows",row03);
            }
        }else {
            //若前端请求无goodsTypeId，直接查询所有数据
            Integer totalPage = (pageNum - 1) * pageSize;
            List<Goods> rows01 =  basicsMessageDao.getGoodListAll(totalPage,pageSize,goodsName,goodsTypeId);
            map.put("rows", rows01);
        }

        return map;

    }

    //10、商品管理-新增分类
    @Override
    public ServiceVO addGoodType(String goodsTypeName, Integer pId) {
        if (pId != null && pId >1){
            Integer goodTypeState = 0;
            basicsMessageDao.addGoodType(goodsTypeName,pId,goodTypeState);
        }else {
            Integer goodTypeState = 1;
            basicsMessageDao.addGoodType(goodsTypeName,pId,goodTypeState);
        }
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    //11、商品管理-删除分类
    @Override
    public ServiceVO deleteGoodType(Integer goodsTypeId) {
        if (goodsTypeId != null) {
            basicsMessageDao.deleteGoodType(goodsTypeId);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }

    //14、根据商品名称或编码查询无库存商品列表
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer pageNo, Integer pageSize, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        //1、总条数
        Integer total = basicsMessageDao.NoInventoryGoodsListTotal(nameOrCode);
        map.put("total", total);


        //2、NoInventoryGoodsList
        List<Goods> NoInventoryGoodsList = basicsMessageDao.getNoInventoryQuantity(pageNo,pageSize,nameOrCode);
        map.put("rows", NoInventoryGoodsList);
        return map;
    }

    //15、根据商品名称或编码查询有库存商品列表
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer pageNo, Integer pageSize, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        //1、总条数
        Integer total = basicsMessageDao.NoInventoryGoodsListTotal(nameOrCode);
        map.put("total", total);


        //2、InventoryGoodsList
        List<Goods> NoInventoryGoodsList = basicsMessageDao.getInventoryQuantity(pageNo,pageSize,nameOrCode);
        map.put("rows", NoInventoryGoodsList);
        return map;
    }
}
